package com.epfresh.supplier.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.epfresh.supplier.R;

/**
 * Created by zhuli on 2017/8/23.
 */

public class CustomDetailDeleteDialog extends Dialog {
    private Button mNegativeBtn;//确定按钮
    private Button mPositiveBtn;//取消按钮
    private TextView mTitleTv;//消息标题文本
    private TextView mMessageTv;//消息提示文本
    private EditText mMessageEt;
    private boolean isTv;
    private String mTitleStr;//从外界设置的title文本
    private String mMessageStr;//从外界设置的消息文本
    //确定文本和取消文本的显示内容
    private String mNegativeStr, mPositiveStr;
    private int color;

    private onNegativeClickListener mOnNegativeClickListener;//取消按钮被点击了的监听器
    private onPositiveClickListener mOnPositiveClickListener;//确定按钮被点击了的监听器

    public String getMessageEt() {
        return mMessageEt.getText().toString().trim();
    }

    /**
     * 设置取消按钮的显示内容和监听
     */
    public void setOnNegativeClickListener(String str, onNegativeClickListener mOnNegativeClickListener) {
        if (!TextUtils.isEmpty(str)) {
            mNegativeStr = str;
        }
        this.mOnNegativeClickListener = mOnNegativeClickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     */
    public void setOnPositiveClickListener(String str, onPositiveClickListener mOnPositiveClickListener) {
        if (!TextUtils.isEmpty(str)) {
            mPositiveStr = str;
        }
        this.mOnPositiveClickListener = mOnPositiveClickListener;
    }

    public CustomDetailDeleteDialog(Context context, boolean isTv) {
        super(context, R.style.PosDialog);
        this.isTv = isTv;
    }

    public CustomDetailDeleteDialog(Context context, boolean isTv, int color) {
        super(context, R.style.PosDialog);
        this.isTv = isTv;
        this.color = color;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_customer_detail_delete);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        mPositiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnPositiveClickListener != null) {
                    mOnPositiveClickListener.onPositiveClickListener();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        mNegativeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNegativeClickListener != null) {
                    mOnNegativeClickListener.onNegativeClickListener();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(mTitleStr)) {
            mTitleTv.setText(mTitleStr);
        } else {
            mTitleTv.setVisibility(View.GONE);
        }
        if (isTv) {
            if (mMessageStr != null) {
                mMessageTv.setText(mMessageStr);
                if (color != 0) {
                    mMessageTv.setTextSize(20);
                    mMessageTv.setTextColor(getContext().getResources().getColor(R.color.red));
                    mMessageTv.setGravity(Gravity.CENTER);
                }
            }
        } else {
            if (mMessageStr != null) {
                mMessageEt.setText(mMessageStr);
                mMessageEt.setVisibility(View.VISIBLE);
                mMessageTv.setVisibility(View.GONE);
            }
        }
        //如果设置按钮的文字
        if (!TextUtils.isEmpty(mPositiveStr)) {
            mPositiveBtn.setText(mPositiveStr);
        }
        if (!TextUtils.isEmpty(mNegativeStr)) {
            mNegativeBtn.setText(mNegativeStr);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mPositiveBtn = (Button) findViewById(R.id.positive_btn);
        mNegativeBtn = (Button) findViewById(R.id.negative_btn);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mMessageEt = (EditText) findViewById(R.id.message_et);
        mMessageTv = (TextView) findViewById(R.id.message_tv);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mTitleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        mMessageStr = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onNegativeClickListener {
        void onNegativeClickListener();
    }

    public interface onPositiveClickListener {
        void onPositiveClickListener();
    }

}
